package HP.com.guess.wangyu;

import java.util.Scanner;

public class Games {

    //开始游戏
    public void startGame() {
        showUI(0, null, null);
        /* -------------猜拳--------------
           规则:1.剪刀 2.石头 3.布
           请输入对方角色:*/
        String computer_name = inPutStr();//电脑角色
        showUI(1, null, null);/*请输入您的角色名:*/
        String user_name = inPutStr(); //用户角色
        loadGames(computer_name, user_name);//加载游戏
    }

    //加载游戏
    public void loadGames(String computer_name, String user_name) {
        Computer computer = new Computer(computer_name, 0);//初始化电脑对象
        User user = new User(user_name, 0);//初始化用户对象
        showUI(2, user.getName(), computer.getName());/*要开始吗?(y/s)*/
        showUI(13, null, null);/*--------------------*/
        String is_start = inPutStr();

        boolean con_boo = true;
        if (is_start.equals("n")) {
            startGame();//重新开始
        } else if (is_start.equals("y")) { //确定开始游戏
            do {
                gameMain(computer, user);//调用游戏本体
                showUI(10, null, null);/*是否继续游戏?(y/n)*/
                String con_str = inPutStr();
                if (con_str.equals("y")) {
                } else if (con_str.equals("n")) {
                    con_boo = false;
                    showResult(computer, user); //计算游戏结果
                    showUI(12, null, null);/*是否继续游戏?(y/n)*/
                    showUI(13, null, null);/*--------------*/
                    String str_exit = inPutStr();
                    if (str_exit.equals("y")) {
                        exitGame();//退出
                    } else if (str_exit.equals("n")) {
                        Games games = new Games();
                        games.startGame();//创建对象,重新开始
                    } else {
                        showUI(4, null, null);/*您的输入有误!*/
                        exitGame();//暂时不处理此BUG;
                    }
                } else {
                    showUI(4, null, null);/*您的输入有误!*/
                    exitGame();//暂时不处理此BUG;
                }
            } while (con_boo);
        } else {
            showUI(4, null, null);/*您的输入有误!*/
            exitGame();
        }
    }

    //游戏本体
    public void gameMain(Computer computer, User user) {

        showUI(3, null, null);/*该你出拳了:(规则:1.剪刀 2.石头 3.布)*/
        int user_punch = user.userHand();
        showUI(5, user.getName(), intToString(user_punch));/*用户出拳:*/

        int computer_punch = computer.computerHand();
        showUI(6, intToString(computer_punch), computer.getName());/*电脑出拳:*/

        if (countResult(user_punch, computer_punch) == 1) {
            showUI(7, user.getName(), null); /*用户赢了*/
            showUI(13, null, null);/*-----------------*/
            user.setScore(user.getScore() + 1);//将用户成绩+1
        } else if (countResult(user_punch, computer_punch) == -1) {
            showUI(8, null, computer.getName());/*电脑赢了*/
            showUI(13, null, null);/*-----------------*/
            computer.setScore(computer.getScore() + 1);//将用户成绩+1
        } else if (countResult(user_punch, computer_punch) == 0) {
            showUI(9, null, null);/*平局*/
            showUI(13, null, null);/*-----------------*/
        }
    }

    //计算对战结果
    public int countResult(int user, int computer) {
        /**
         * 剪刀->布   石头->剪刀  布->石头
         * 1 用户赢; -1 电脑赢 (规则:1.剪刀 2.石头 3.布); 0 平局
         */
        if ((user == 1 && computer == 3) || (user == 2 && computer == 1) || (user == 3 && computer == 2)) {
            return 1;
        } else if ((user == 1 && computer == 2) || (user == 2 && computer == 3) || (user == 3 && computer == 1)) {
            return -1;
        } else if ((user == 1 && computer == 1) || (user == 2 && computer == 2) || (user == 3 && computer == 3)) {
            return 0;
        } else {
            System.err.println("对战出现错误!");
            return 404;
        }
    }

    //显示游戏结果
    public void showResult(Computer computer, User user) {
        showUI(11, user.getName(), ("" + user.getScore()));/*用户得分: 10分*/
        showUI(11, computer.getName(), ("" + computer.getScore()));/*电脑得分: 10分*/
        if (user.getScore() > computer.getScore()) {
            showUI(8, null, user.getName());/*用户赢了*/
        } else if (user.getScore() < computer.getScore()) {
            showUI(8, null, computer.getName());/*电脑赢了*/
        } else {
            showUI(9, null, null);
        }
    }

    //显示UI
    public void showUI(int ui, String user, String computer) {
        switch (ui) {
            case 0:
                System.out.println("-------------猜拳--------------");
                System.out.println("规则:1.剪刀 2.石头 3.布");
                System.out.println("请输入对方角色:");
                break;
            case 1:
                System.out.println("请输入您的角色名:");
                break;
            case 2:
                System.out.println(user + "VS" + computer);
                System.out.println("要开始吗?(y/n)");
                break;
            case 3:
                System.out.println("该你出拳了:(规则:1.剪刀 2.石头 3.布)");
                break;
            case 4:
                System.out.println("您的输入有误!");
                break;
            case 5:
                System.out.println(user + "出拳:" + computer);
                break;
            case 6:
                System.out.println(computer + "出拳:" + user);
                break;
            case 7:
                System.out.println(user + "赢了!");
                break;
            case 8:
                System.out.println(computer + "赢了!");
                break;
            case 9:
                System.out.println("平局!");
                break;
            case 10:
                System.out.println("是否继续游戏?(y/n)");
                break;
            case 11:
                System.out.println(user + "得分:" + computer + "分");
                break;
            case 12:
                System.out.println("是否退出游戏?(y/n)");
                break;
            case 13:
                System.out.println("------------------------------");
                break;
            default:
        }
    }

    //数字出拳转字符串出拳
    public String intToString(int num) {
        if (num == 1)
            return "剪刀";
        else if (num == 2)
            return "石头";
        else if (num == 3)
            return "布";
        else
            return "输入有误!";
    }

    //输入字符串
    public String inPutStr() {
        Scanner scanner = new Scanner(System.in);
        String input_str = scanner.nextLine();
        return input_str;
    }

    //退出
    public void exitGame() {
        System.exit(0);
    }
}


